package com.zpark.controller;

import com.zpark.mapper.ISearchMapper;
import com.zpark.pojo.Search;
import com.zpark.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoyu
 * @version 1.0 2022/11/19
 */
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private ISearchService iSearchService;

    @Autowired
    private ISearchMapper iSearchMapper;

    static List<Search> content = new ArrayList<>();
    static Integer count;

    @RequestMapping("/hotword")
    public ModelAndView findByHotWord(String hotword) {
        // 创建视图解析器
        ModelAndView mv = new ModelAndView();
        // 设置视图名字
        mv.setViewName("index");

        if(hotword.equals("热点")) {
            // 获取数据
            content = iSearchService.findByReDian(hotword);
            count = iSearchMapper.selectReDianCount();
            // 绑定数据
            mv.addObject("hot", hotword);
            mv.addObject("content", content);
            mv.addObject("count", count);
            System.err.println(hotword);
        } else if(hotword.equals("百度")) {
            // 获取数据
            content = iSearchService.findByBaidu(hotword);
            count = iSearchMapper.selectBaiduCount();
            // 绑定数据
            mv.addObject("hot", hotword);
            mv.addObject("content", content);
            mv.addObject("count", count);
            System.err.println(hotword);
        } else if(hotword.equals("抖音")) {
            // 获取数据
            content = iSearchService.findByDouYin(hotword);
            count = iSearchMapper.selectDouYinCount();
            // 绑定数据
            mv.addObject("hot", hotword);
            mv.addObject("content", content);
            mv.addObject("count", count);
            System.err.println(hotword);
        } else if(hotword.equals("java")) {
            // 获取数据
            content = iSearchService.findByJava(hotword);
            count = iSearchMapper.selectJavaCount();
            // 绑定数据
            mv.addObject("hot", hotword);
            mv.addObject("content", content);
            mv.addObject("count", count);
            System.err.println(hotword);
        } else if(hotword.equals("笑话")) {
            // 获取数据
            content = iSearchService.findByXiaoHua(hotword);
            count = iSearchMapper.selectXiaoHuaCount();
            // 绑定数据
            mv.addObject("hot", hotword);
            mv.addObject("content", content);
            mv.addObject("count", count);
            System.err.println(hotword);
        } else if(hotword.equals("工程师")) {
            // 获取数据
            content = iSearchService.findByEngineer(hotword);
            count = iSearchMapper.selectEngineerCount();
            // 绑定数据
            mv.addObject("hot", hotword);
            mv.addObject("content", content);
            mv.addObject("count", count);
            System.err.println(hotword);
        } else if(hotword.equals("食物")) {
            // 获取数据
            content = iSearchService.findByFood(hotword);
            count = iSearchMapper.selectFoodCount();
            // 绑定数据
            mv.addObject("hot", hotword);
            mv.addObject("content", content);
            mv.addObject("count", count);
            System.err.println(hotword);
        }
        return mv;
    }

    @RequestMapping("/any")
    public ModelAndView searchAny(HttpServletRequest request) {
        String word = request.getParameter("word");
        // 创建视图解析器
        ModelAndView mv = new ModelAndView();
        // 设置视图名字
        mv.setViewName("any");
        // 获取数据
//        List<Search> content = iSearchService.findBySearchTerms(word);
//        Integer count = iSearchService.getCount(word);
        content = iSearchService.findBySearchTerms(word);
        count = iSearchService.getCount(word);
        // 绑定数据
        mv.addObject("word", word);
        mv.addObject("content", content);
        mv.addObject("count", count);
        System.err.println(word);
        return mv;
    }

    @RequestMapping("/delete")
    public ModelAndView deleteById(Integer id, HttpServletRequest request, String word) {
        Integer row = iSearchMapper.deleteById(id);
        // 创建视图解析器
        ModelAndView mv = new ModelAndView();
        // 设置视图名字
        mv.setViewName("back");
        // 获取数据
//        List<Search> content = iSearchService.findBySearchTerms(word);
//        Integer count = iSearchService.getCount(word);
        content = iSearchService.findBySearchTerms(word);
        count = iSearchService.getCount(word);
        // 绑定数据
        mv.addObject("word", word);
        mv.addObject("content", content);
        mv.addObject("count", count);
        System.err.println(word);
        return mv;
    }


    @RequestMapping("/back")
    public ModelAndView searchForBack(HttpServletRequest request) {
        String word = request.getParameter("word");
        // 创建视图解析器
        ModelAndView mv = new ModelAndView();
        // 设置视图名字
        mv.setViewName("back");
        // 获取数据
//        List<Search> content = iSearchService.findBySearchTerms(word);
//        Integer count = iSearchService.getCount(word);
        content = iSearchService.findBySearchTerms(word);
        count = iSearchService.getCount(word);
        // 绑定数据
        mv.addObject("word", word);
        mv.addObject("content", content);
        mv.addObject("count", count);
        System.err.println(word);
        return mv;
    }

}
